// pages/equipment_details/equipment_details.js

import Dialog from '@vant/weapp/dialog/dialog';

var config = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    renameShow: false,
    redevname: "",
    uuid: "",
    phone: "",
    devicesid: "",
    info: []
  },

  onDeldev(e) {
    console.log(e);
    var _this = this


    Dialog.confirm({
      title: '删除设备',
      message: '确定删除该设备?'
    }).then(() => {
      // on confirm
      // console.log('on confirm phone:', _this.data.phone, "uuid", _this.data.uuid);
      wx.request({
        url: config.url.DelDev,
        method: 'POST',
        data: {
          'phone': _this.data.phone,
          'uuid': _this.data.uuid,
          client: config.client,
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
        success(res) {
          console.log(res)
          if (res.data.code == 200) {
            // 关闭修改设备名称弹窗
            // _this.onClose()

            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 2000
            })

            wx.reLaunch({
              url: '../index/index?id=' + _this.data.phone,
            })

          } else {
            wx.showToast({
              title: '删除失败',
              icon: '',
              duration: 2000
            })
          }
        },
      })
    }).catch(() => {
      // on cancel
      // console.log('on cancel');
    });
  },

  onClose(e) {
    console.log(e);
    this.setData({
      renameShow: false,
      redevname: ''
    })
  },
  // 确认修改设备名称
  onConfirm(e) {
    // 得到修改值
    var _this = this
    let value = this.data.redevname
    console.log(value, value.length)

    if (value.length > 0) {
      // try {
      //   var phone = wx.getStorageSync('phone')
      // } catch (e) {
      //   // Do something when catch error
      // }
      // 提交数据 需替换地址及参数
      wx.request({
        url: config.url.DevRename,
        method: 'POST',
        data: {
          'phone': _this.data.phone,
          'uuid': _this.data.uuid,
          'name': value,
          client: config.client,
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
        success(res) {
          console.log(res)
          if (res.data.code == 200) {
            // 关闭修改设备名称弹窗
            _this.onClose()

            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: '修改失败',
              icon: '',
              duration: 2000
            })
          }
        },
      })
    }

  },

  onRenameChange(e) {
    console.log(e);
    this.setData({
      // renameShow: false
      redevname: e.detail
    })
  },
  to_reuser() {
    console.log("to_reuser")
    wx.navigateTo({
      url: '../reusername/reusername?uuid=' + this.data.uuid + '&phone=' + this.data.phone,
    })
  },

  to_redevname() {
    var _this = this
    _this.setData({
      renameShow: true
    })
  },

  to_shareqrcode() {
    //timestamp
    var timestamp = new Date().getTime();
    wx.navigateTo({
      url: '../shareqrcode/shareqrcode?uuid=' + this.data.uuid + '&phone=' + this.data.phone + '&timestamp=' + timestamp + '&code=' + 200,
    })
  },
  to_shareuser() {
    wx.navigateTo({
      url: '../shareuser/shareuser?uuid=' + this.data.uuid + '&phone=' + this.data.phone,
    })
  },
  to_devmessage() {
    wx.navigateTo({
      url: '../devmessage/devmessage?uuid=' + this.data.uuid + '&phone=' + this.data.phone,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options:", options)
    console.log("uuid:", options.uuid)
    console.log("uuidid:", options.uuid.substring(8))
    try {
      var phone = wx.getStorageSync('phone')
    } catch (e) {
      // Do something when catch error
    }

    this.setData({
      phone: phone,
      uuid: options.uuid,
      devicesid: options.uuid.substring(8),
      host_user: options.host_user,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var _this = this
    wx.request({
      url: config.url.getDevInfo,
      method: 'POST',
      data: {
        'uuid': this.data.uuid,
        'phone': this.data.phone,
        client: config.client,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
      },
      success(res) {
        console.log(res)
        // 调试代码
        //res.data.data[0].host_user = true
        if (res.data.code == 0) {
          _this.setData({
            info: res.data.data[0]
          })
        }
      },
      fail(err) {
        console.err(err)
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }

})